package com.spring.daoImpl;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.basic.daoImpl.BaseDaoImpl;
import com.spring.dao.UserDao;
import com.spring.model.User;

@Repository(value="userDao")
public class UserDaoImpl extends BaseDaoImpl implements UserDao {
	
	@Override
	public User login(String name, String password) {
		// TODO Auto-generated method stub
		if(name=="1" && password =="1") {
			System.out.println(name);
			return new User(name,password);
		}
		return null;
	}

	@Override
	public void outMoney(final String out, final int money) {
		// TODO Auto-generated method stub
		String sql = "UPDATE USER SET money=money-? WHERE username=?";
		getJdbcTemplate().update(sql, new PreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				// TODO Auto-generated method stub
				ps.setInt(1, money);
				ps.setString(2, out);
			}
		});
	}

	@Override
	public void inMoney(final String in, final int money) {
		// TODO Auto-generated method stub
		String sql = "UPDATE USER SET money=money+? WHERE username=?";
		getJdbcTemplate().update(sql, new PreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				// TODO Auto-generated method stub
				ps.setInt(1, money);
				ps.setString(2, in);
			}
		});
	}

}
